package com.zoffy.ybuhelper.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.zoffy.ybuhelper.R;
import org.jetbrains.annotations.NotNull;

public class HomeFragment extends UpdateFragment {

    private TextView schoolNumber;
    private SharedPreferences preferences;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view=inflater.inflate(R.layout.fragment_home,null,false);
        init(view);
        return view;
    }


    public void showLoginFragment(){
        LoginFragment loginFragment=new LoginFragment();
        loginFragment.show(getChildFragmentManager(),"LoginFragment");
    }
    public void gptLoginFragment(){
        GPTLoginFragment loginFragment=new GPTLoginFragment();
        loginFragment.show(getChildFragmentManager(),"GPTLoginFragment");
    }

    public void init(View view){

        LinearLayout accountBind =view.findViewById(R.id.account_bind);
        accountBind.setOnClickListener(v-> showLoginFragment());
        LinearLayout gptBind=view.findViewById(R.id.firegpt_bind);
        gptBind.setOnClickListener(v->gptLoginFragment());
        preferences = getActivity().getSharedPreferences("user_data", Context.MODE_PRIVATE);
        schoolNumber = view.findViewById(R.id.school_number);
        update();

    }

    @Override
    public void update() {

        String account=preferences.getString("account","");
        schoolNumber.setText("".equals(account)?"未绑定":account);

    }
}
